﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using He.Web.UI;
using He.Common;

namespace He.Web.manage.settings
{
    public partial class contents_list : He.Web.UI.Manage
    {
        public int channel_id;                                //栏目ID
        public string keywords = string.Empty;                //关键字
        protected int pageSize = 12;                          //每页记录数
        protected int page;                                   //当前页
        protected int pcount;                                 //总页数

        protected void Page_Load(object sender, EventArgs e)
        {
            page = GetInt("page", 0);
            channel_id = GetInt("channel_id", 0);
            keywords = GetString("keywords");
            if (!IsPostBack)
            {
                ddlBind();
                RptBind(Comb_SqlTxt(this.channel_id, this.keywords));
            }
        }

        #region 数据绑定
        private void RptBind(string _strWhere)
        {
            this.ddlChannelId.SelectedValue = this.channel_id.ToString();
            this.txtKeywords.Text = this.keywords;

            BLL.article bll = new BLL.article();
            rptList.DataSource = bll.GetPageList(pageSize, page, _strWhere, "category_id asc,sort_id asc,ID desc");
            rptList.DataBind();
            pcount = bll.GetRecordCount(_strWhere);
        }
        #endregion

        #region 组合SQL查询语句
        protected string Comb_SqlTxt(int _channel_id, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            strTemp.AppendFormat(" category_id in(select id from {0}category where channel_id in(select id from {0}channel where model_id={1}))", SysKeys.TABLE_PREFIX, (int)Enums.sys_model.内容模块);
            _keywords = _keywords.Replace("'", "");
            if (channel_id > 0)
            {
                strTemp.Append(" and category_id in (select Id from he_category where channel_id=" + _channel_id + ")");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and title like '%" + _keywords + "%'");
            }
            strTemp.AppendFormat(" and category_id in(select id from he_category where channel_id in(select id from he_channel where lang='{0}'))", lang);
            return strTemp.ToString();
        }
        #endregion

        #region 组合URL语句
        protected string Comb_UrlTxt(int _channel_id, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (_channel_id > 0)
            {
                strTemp.Append("channel_id=" + _channel_id + "&");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append("keywords=" + HttpContext.Current.Server.UrlEncode(_keywords) + "&");
            }
            strTemp.AppendFormat("lang={0}&", lang);
            return strTemp.ToString().Trim('&');
        }
        #endregion

        #region 单页频道绑定
        protected void ddlBind()
        {
            DataTable dt = new He.BLL.channel().GetList(string.Format("model_id={0} and lang='{1}'", (int)Enums.sys_model.内容模块, lang)).Tables[0];
            ddlChannelId.Items.Add(new ListItem("所有类别", ""));
            foreach (DataRow dr in dt.Rows)
            {
                ddlChannelId.Items.Add(new ListItem(dr["title"].ToString(), dr["id"].ToString()));
            }
        }
        #endregion

        //频道
        protected void ddlChannelId_SelectedIndexChanged(object sender, EventArgs e)
        {
            int _channel_id;
            if (int.TryParse(ddlChannelId.SelectedValue, out _channel_id))
            {
                Response.Redirect("contents_list.aspx?" + Comb_UrlTxt(_channel_id, this.keywords));
            }
            else
            {
                Response.Redirect("contents_list.aspx?" + Comb_UrlTxt(0, this.keywords));
            }
        }

        //搜索
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Response.Redirect("contents_list.aspx?" + Comb_UrlTxt(this.channel_id, this.txtKeywords.Text.Trim()));
        }

        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            BLL.article bll = new BLL.article();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                Model.article model = bll.GetModel(id);
                if (cb.Checked)
                {
                    //删除
                    if (model.is_sys == 1)
                    {
                        JscriptMsg(350, 230, "操作失败", "<b>错误提示！</b>您删除的内容页已被保护，请先取消保护。", "back", "Error");
                        continue;
                    }
                    if (bll.Delete(id))
                    {
                        SaveLogs("[内容模块]删除内容页：" + model.title);
                    }
                }
            }
            JscriptPrint("批量删除成功啦！", Request.RawUrl, "Success");
        }

        //保存排序
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            He.BLL.article bll = new He.BLL.article();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            JscriptPrint("排序保存成功啦！", Request.RawUrl, "Success");
        }
    }
}